<template>
<!--  根据条件查询-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="猪只耳号" prop="pigCode">
        <el-input
          v-model="queryParams.pigCode"
          placeholder="请输入猪只耳号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转出栏舍类型" prop="transferOutPhouseType">
        <el-select
          v-model="queryParams.transferOutPhouseType"
          placeholder="转出栏舍类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.hmy_park_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="转入栏舍类型" prop="transferIntoPhouseType">
        <el-select
          v-model="queryParams.transferIntoPhouseType"
          placeholder="转入栏舍类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.hmy_park_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="转舍时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--  按钮 设置-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hmy:pigTransfer:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hmy:pigTransfer:update']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hmy:pigTransfer:del']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--加载信息列表-->
    <el-table v-loading="loading" :data="pigTransferList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="母猪耳号" prop="pigCode" width="80" align="center"/>
      <el-table-column label="猪只类型" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_pig_type" :value="scope.row.pigType"/>
        </template>
      </el-table-column>

      <el-table-column label="转出栏舍类型" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_park_type" :value="scope.row.transferOutPhouseType"/>
        </template>
      </el-table-column>
      <el-table-column label="转出栏舍编码" prop="transferOutPhouseCode" width="100" align="center"/>
      <el-table-column label="转入栏舍类型" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_park_type" :value="scope.row.transferIntoPhouseType"/>
        </template>
      </el-table-column>
      <el-table-column label="转入栏舍编码" prop="transferIntoPhouseCode" width="100" align="center"/>


      <el-table-column label="转舍猪只数量" prop="transferPigNum" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.transferPigNum + '只' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转舍猪只重量" prop="transferPigWeight" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.transferPigWeight + '斤' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="转舍日期" prop="transferDate" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.transferDate.slice(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="creationDate" width="150">
        <template slot-scope="scope">
          {{ (scope.row.creationDate.slice(0, 10)) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.transferId !== 0">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hmy:pigTransfer:update']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hmy:pigTransfer:del']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改-->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="猪只耳号" prop="pigCode">
              <el-input v-model="form.pigCode" placeholder="请输入母猪耳号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="猪只类型" prop="pigType">
              <el-select
                v-model="form.pigType"
                placeholder="请选择猪只类型"
                style="width: 280px"
              >
                <el-option
                  v-for="dict in dict.type.hmy_pig_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>


        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="转出栏舍类型" prop="transferOutPhouseType">
              <el-select
                v-model="form.transferOutPhouseType"
                placeholder="请选择转出栏舍类型"
                style="width: 280px"
              >
                <el-option
                  v-for="dict in dict.type.hmy_park_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转出栏舍编码" prop="transferOutPhouseCode">
              <el-input v-model="form.transferOutPhouseCode" placeholder="请输入转出栏舍编码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="转入栏舍类型" prop="transferIntoPhouseType">
              <el-select
                v-model="form.transferIntoPhouseType"
                placeholder="请选择转入栏舍类型"
                style="width: 280px"
              >
                <el-option
                  v-for="dict in dict.type.hmy_park_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转入栏舍编码" prop="transferIntoPhouseCode">
              <el-input v-model="form.transferIntoPhouseCode" placeholder="请输入转入栏舍编码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="转舍数量" prop="transferPigNum">
              <el-input v-model="form.transferPigNum" placeholder="转舍(单位  只)"/>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="重量" prop="transferPigWeight">
              <el-input v-model="form.transferPigWeight" placeholder="重量(单位  斤)"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="转舍日期" prop="transferDate">
              <el-date-picker
                v-model="form.transferDate"
                placeholder="请选择转舍日期"
                style="width: 280px"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

import { listPigTransfer,getPigTransfer, addPigTransfer, updatePigTransfer, delPigTransfer} from "@/api/hmy/pigTransfer";
export default {
  name: "PigTransfer",
  dicts: ['hmy_pig_type','hmy_park_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进货表格数据
      pigTransferList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pigType: undefined,
        pigCode: undefined,
        topt: undefined,
        transferOutPhouseCode: undefined,
        tipt: undefined,
        transferIntoPhouseCode: undefined,
        transferPigNum: undefined,
        transferPigWeight: undefined,
        dateStart: undefined,
        dateEnd: undefined

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pigCode: [
          {required: true, message: "猪只耳号不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询猪只档案列表 */
    getList() {
      this.loading = true;
      this.queryParams.dateStart = this.dateRange[0];
      this.queryParams.dateEnd = this.dateRange[1];
      listPigTransfer(this.queryParams).then(response => {
          this.pigTransferList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        pigCode: undefined,
        topt: undefined,
        tipt: undefined,
        transferDate: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      //this.resetForm("queryForm");
      this.queryParams = {};
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.transferId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    //添加按钮
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加猪只转舍记录信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const transferId = row.transferId || this.ids
      getPigTransfer(transferId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改猪只转舍记录信息";

        console.log(response.data);
        console.log(response);
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.transferId != undefined) {

            updatePigTransfer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPigTransfer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const transferIds = row.transferId || this.ids;
      this.$modal.confirm('是否确认删除猪只分娩记录编号为"' + transferIds + '"的数据项？').then(function () {
        return delPigTransfer(transferIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
